package com.sara.controller;

import com.github.pagehelper.PageInfo;
import com.sara.controller.exceptionHandler.NotFoundException;
import com.sara.entity.Employee;
import com.sara.entity.Parts;
import com.sara.entity.Type;
import com.sara.service.PartsService;
import com.sara.service.TypeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Wayne
 * @date 2019/1/17
 */
@Controller
@RequestMapping("/parts")
public class PartsController {

    private Logger logger = LoggerFactory.getLogger(PartsController.class);

    @Autowired
    private PartsService partsService;

    @Autowired
    private TypeService typeService;



    @GetMapping
    public String list(@RequestParam(name = "p", defaultValue = "1", required = false) Integer pageNum,
                       String partsName,
                       Integer partsType,
                       Integer partsInventory,
                       Model model){


        List<Type> typeList = typeService.findAll();
        model.addAttribute("typeList", typeList);

        Map<String, Object> params = new HashMap<>();
        params.put("partsType", partsType);
        params.put("partsName", partsName);
        params.put("partsInventory", partsInventory);

        PageInfo<Parts> page =  partsService.findByParams(pageNum, params);

        model.addAttribute("page" , page);
        return "parts/list";
    }


    @GetMapping("/new")
    public String partsNew(Model model){
        List<Type> typeList = typeService.findAll();
        model.addAttribute("typeList", typeList);
        return "parts/new";
    }

    @PostMapping("/new")
    public String partsNew(Parts parts, RedirectAttributes attributes){
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        Parts parts1 = partsService.findByPartsNo(parts.getPartsNo());
        if(parts1 != null){
            partsService.update(parts);
        }else {
            partsService.save(parts);
        }

        attributes.addFlashAttribute("message", "入库成功");
        return "redirect:/parts";
    }

    @GetMapping("/edit/{id:\\d+}")
    public String editPage(@PathVariable Integer id, Model model){
       Parts parts = partsService.findById(id);
        List<Type> typeList = typeService.findAll();
        if(parts == null){
            throw new NotFoundException();
       }

        model.addAttribute("typeList", typeList);
        model.addAttribute("parts", parts);
        return "parts/edit";
    }

    @PostMapping("/edit")
    public String EditPage(Parts parts, RedirectAttributes  attributes){
        partsService.update(parts);
        attributes.addFlashAttribute("message","修改成功");
        return "redirect:/parts";
    }

    @GetMapping("/delete/{id:\\d+}")
    public String delete(@PathVariable Integer id, RedirectAttributes attributes){
        Parts parts = partsService.findById(id);
        if(parts == null ){
            throw new NotFoundException();
        }
        logger.debug("删除配件：", parts);
        partsService.deleteById(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/parts";
    }
}
